package cn.gov.pbc.gd.credit.log.service.log.impl;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.liu.jbase.ResultVo;
import com.liu.jbase.constant.ResCode;
import com.liu.tools.LogUtil;

import cn.gov.pbc.gd.credit.api.log.ILogInquiryApi;
import cn.gov.pbc.gd.credit.api.log.ILogLoginApi;
import cn.gov.pbc.gd.credit.api.log.ILogOperateApi;
import cn.gov.pbc.gd.credit.log.service.log.IMongoService;
import cn.gov.pbc.gd.credit.vo.log.LogInquiryVo;
import cn.gov.pbc.gd.credit.vo.log.LogLoginVo;
import cn.gov.pbc.gd.credit.vo.log.LogOperateVo;

@Service
public class MongoServiceImpl implements IMongoService {

	private static Logger logger = LoggerFactory.getLogger(MongoServiceImpl.class);
	
	@Autowired
	private ILogInquiryApi logInquiryApi;
	
	@Autowired
	private ILogOperateApi logOperateApi;
	
	@Autowired
	private ILogLoginApi logLoginApi;
	
	private static Executor loginExecutor = Executors.newFixedThreadPool(50);
	
	private static Executor operateExecutor = Executors.newFixedThreadPool(50);
	
	private static Executor inquiryExecutor = Executors.newFixedThreadPool(100);
	
	@Override
	public ResultVo<?> addLogLogin(LogLoginVo vo, HttpServletRequest request) {
		
		operateExecutor.execute(new Runnable() {
			
			@Override
			public void run() {
				try {
					if(vo.getLoginResult() == 1){
//						LogLoginVo temp = new LogLoginVo();
//						temp.setUserId(vo.getUserId());
//						temp.setQueryFlag(1);
//						temp = logLoginApi.getOne(temp);
//						if(temp != null){
//							long curDate = new Date().getTime();
//							long lastLoginDate = temp.getLoginTime().getTime();
//							if((curDate - lastLoginDate) > 30 * 24 * 60 *60 * 100){
//								vo.setNote("睡眠账户，该账户已有30天未登录系统");
//							}
//						} else {
//							vo.setNote("用户首次登录");
//						}
					}
					logLoginApi.insert(vo);
				} catch (Exception e) {
					logger.error(LogUtil.msg("MongoServiceImpl:addLogLogin", "添加登录日志异常,reqData=" + JSON.toJSONString(vo)), e);
				}
				
			}
		});
		
		ResultVo<?> resultVo = new ResultVo<>();
		resultVo.setCode(ResCode.SUCCESS);
		
		return resultVo;
	}

	@Override
	public ResultVo<?> addLogOperate(LogOperateVo vo, HttpServletRequest request) {

		loginExecutor.execute(new Runnable() {
			
			@Override
			public void run() {
				try {
					logOperateApi.insert(vo);
				} catch (Exception e) {
					logger.error(LogUtil.msg("MongoServiceImpl:addLogOperate", "添加操作日志异常,reqData=" + JSON.toJSONString(vo)), e);
				}
				
			}
		});
		
		ResultVo<?> resultVo = new ResultVo<>();
		resultVo.setCode(ResCode.SUCCESS);
		
		return resultVo;
	}

	@Override
	public ResultVo<?> addLogInquiry(LogInquiryVo vo, HttpServletRequest request) {

		inquiryExecutor.execute(new Runnable() {
			
			@Override
			public void run() {
				try {
					logInquiryApi.insert(vo);
				} catch (Exception e) {
					logger.error(LogUtil.msg("MongoServiceImpl:addLogOperate", "添加查询日志异常,reqData=" + JSON.toJSONString(vo)), e);
				}
				
			}
		});
		
		ResultVo<?> resultVo = new ResultVo<>();
		resultVo.setCode(ResCode.SUCCESS);
		
		return resultVo;
	}

}
